var app = getApp()
Page({
  data: {
    url: {
      comment: app.url.baseUrl + "/indent/comment",
      getDetail: app.url.baseUrl +"/my/indent/detail",
      upload:"https://apis.app.ltaste.cn/imageupload",
    },
    starts: [false, false, false, false, false],
    text: "",
    images: [],
    tj:true,
  },
  onLoad: function (options) {
    var indentNo = options.indentNo
    console.log(indentNo)
    this.setData({ indentNo: indentNo })
    this.getDetail()
   
  },
  //获取信息
  getDetail: function () {
    var data = { id: this.data.indentNo }
    var that = this;
    app.ajax(this.data.url.getDetail, data, function (res) {
      var d = res.data.data
      wx.setNavigationBarTitle({
        title: d.shopName
      })
      that.setData({ indent: d})
    })
  },
  //选中星星
  selectStart: function (e) {
    var il = e.currentTarget.dataset.startin
    var s = this.data.starts
    if (s[il - 1]) {
      il = il - 1
    }

    for (var j = 0; j < s.length; j++) {
      s[j] = false
    }

    for (var i = 0; i < il; i++) {
      s[i] = true
    }
    this.setData({ starts: s })
  },
  //用户输入内容
  bindTextAreaBlur: function (e) {
    var text = e.detail.value
    this.setData({ text: text })
  },
  //显示图片
  upload: function () {
    var that = this;
    var images = this.data.images
    var n = 0;
    for (var j = 0; j < images.length; j++) {
      var temp = images[j]
      if (temp != null)
        n += 1
    }
    if (n == 0)
      n = 3
    else if (n >= 3)
      return
    else
      n = 3 - n
    wx.chooseImage({
      count: n,
      sizeType: "compressed",
      success: function (res) {
        console.log(res);
        var tempFilePaths = res.tempFilePaths
        for (var i = 0; i < tempFilePaths.length; i++) {
          images.push(tempFilePaths[i])
        }
        that.setData({ images: images })
      },
    })
  },
  //提交
  fcomment: function (e) {
    var that = this
    var images = this.data.images
    var starts = this.data.starts
    var startNum = 0
    for (var i = 0; i < starts.length; i++) {
      if (starts[i])
        startNum += 1
    }

    var tips = null
    if(startNum<=0)
      tips = "请选择评分"
    if(this.data.text.length<1)
      tips ="请填写评论"

    if(tips != null){
      wx.showToast({
        title: tips,
      })
      return
    }


    wx.showLoading({
      title:"提交中...",
      mask:true,
    })


    //图片要上传
    var u = "";
    var imgNum = 0
    var uploadNum = 0
    for (var j = 0; j < images.length; j++) {
      var tempimage = images[j]
      if (tempimage != null){
        imgNum+=1
        wx.uploadFile({
          url: this.data.url.upload,
          filePath: tempimage,
          name: 'file',
          success: function (res) {
            var d = JSON.parse(res.data)
            u += d.url + ",";
            uploadNum += 1
          },fail:function(){
            uploadNum += 1
          }
        })
      }
    }

    
    
    var taht = this;
    setInterval(function(){
      if (imgNum != uploadNum || !taht.data.tj)
        return
      that.setData({tj:false})
      if (u != "")
        u = u.substring(0, u.length - 1);
      console.log("已上传的文件:" + u);
      var indent = taht.data.indent
      if(u.length<=1)
        u=null;
      var data = { indentNo: indent.id, shopId: indent.shopId, startNum: startNum, content: taht.data.text, images: u }
      console.log(data);
      app.ajax(taht.data.url.comment, data, function (res) {
        wx.hideLoading();
        wx.showModal({
          content: '发表成功了',
          showCancel: false,
          success: function () {
            wx.navigateBack({})
          }
        })
      })
    },1000)
  },
  //删除图片
  delImage: function (e) {
    var i = e.currentTarget.dataset.delindex
    var images = this.data.images
    images[i] = null
    this.setData({ images: images });
  }
})